package info.jbohn.model;

import info.jbohn.data.Feed;
import info.jbohn.data.FeedMessage;
import info.jbohn.view.ViewInterface;

import java.util.ArrayList;
import java.util.List;

public class RSSMainModel 
{
	List<Feed> listOfFeed = null;
	List<ViewInterface> viewObserver = null;
	
	
	public RSSMainModel ()
	{
		viewObserver = new ArrayList<>();
		listOfFeed = new ArrayList<>();
	}
	
	public void addObserver(ViewInterface v)
	{
		viewObserver.add(v);
	}
	
	public void removeObserver(ViewInterface v)
	{
		if (!viewObserver.isEmpty())
		{
			viewObserver.remove(v);
		}
	}
	
	public void addFeed(Feed f)
	{
		listOfFeed.add(f);
		notifyUI();
	}
	
	public void removeFeed(Feed f)
	{
		if (!listOfFeed.isEmpty())
		{
			listOfFeed.remove(f);
		}
		notifyUI();
	}
	
	public void notifyUI()
	{
		for( ViewInterface view : viewObserver )
		{
			view.updateUI(this);
		}
	}
	
	public List<Feed> getFeeds()
	{
		return this.listOfFeed;
	}
	
	public List<FeedMessage> getFeedMessages(String feed)
	{
		List<FeedMessage> fm = new ArrayList<>();
		for (Feed f : listOfFeed) 
		{
			if (feed.equalsIgnoreCase(f.getTitle()))
			{
				return f.getMessages(); 
			}
		}
		return fm;
	}
}
